import { reqcartlist, reqcartedit, reqcartdelete } from "../../http/api"
import { Toast } from "antd-mobile"
import { getUserInfo } from "./login"

let isRequest = true;
//state
const initState = {
    // 1.1 列表
    list: [],
    // 2.1
    allChecked: false
}

//action type
const TYPES = {
    // 1.2 修改list type 
    SHOP_CHANGE_LIST: "SHOP_CHANGE_LIST",
    // 2.2 修改全选 
    SHOP_CHANGE_ALL_CHECKED: "SHOP_CHANGE_ALL_CHECKED"
}

//reducer
const reducer = (state = initState, action) => {
    switch (action.type) {
        // 1.3 修改list action={type:"xx",list:[1,2,3]}
        case TYPES.SHOP_CHANGE_LIST:
            return {
                ...state,
                list: [...action.list]
            }
        // 2.3 修改全选 
        case TYPES.SHOP_CHANGE_ALL_CHECKED:
            return {
                ...state,
                allChecked: action.allChecked
            }
        default:
            return state;
    }
}

//action creator
export const actions = {
    //    1.4 修改list action
    changeList: (list) => ({ type: TYPES.SHOP_CHANGE_LIST, list }),
    // 1.6 请求list 
    reqList: () => (dispatch, getState) => {
        let userInfo = getUserInfo(getState())
        reqcartlist({ uid: userInfo.uid }).then(res => {
            let arr = res.data.list ? res.data.list : [];
            //旧的list 
            let list = getList(getState())
            let arr2 = []
        arr.forEach(item=>{
            if(item.content){
                item.content.map(i=>arr2.push(i))
            }
        })
            // 补checked 
            arr2.forEach(item => {
                let obj = list.find(i => i.id == item.id)
                item.checked = obj ? obj.checked : false
            })
            
            //修改list 
            dispatch(actions.changeList(arr2))

            //修改全选
            console.log((arr2.length > 0 ? arr2.every(item => item.checked) : false));
            
            dispatch(actions.changeAllChecked(arr2.length > 0 ? arr2.every(item => item.checked) : false))

            //再次打开请求
            isRequest = true
        })
    },
    //1.7数量+
    add: (id,num) => (dispatch, getState) => {
        if (num >= 5) {
            Toast.show({
                content: "限购五件"
            })
            return;
        }
        if(isRequest){
            isRequest = false
            reqcartedit({
                id,
                type: 2
            }).then(res => {
                if (res.data.code === 200) {
                    Toast.show({
                        content: res.data.msg
                    })
                    //请求列表
                    dispatch(actions.reqList())
                }
            })
        }
        
    },
    // 1.8数量-
    sub: (id, num) => (dispatch) => {
        if (num <= 1) {
            Toast.show({
                content: "宝贝不能再少了"
            })
            return;
        }
        if (isRequest) {
            isRequest = false
            reqcartedit({
                id,
                type: 1
            }).then(res => {
                Toast.show({
                    content: res.data.msg
                })
                //请求列表
                dispatch(actions.reqList())
            })
        }

    },
    //结算
    account:()=>dispatch=>{
        Toast.show({
            icon: 'success',
            content: '结算成功',
        })
    },
    // 1.9 删除
    del: (id) => dispatch => {
        reqcartdelete({ id }).then(res => {
            Toast.show({
                content: res.data.msg
            })
            //请求列表
            dispatch(actions.reqList())
        })
    },
    // 2.4 修改全选  
    changeAllChecked: allChecked => ({ type: TYPES.SHOP_CHANGE_ALL_CHECKED, allChecked }),

    // 2.5 点了全选 
    changeAll: () => (dispatch, getState) => {
        let allChecked = getAllChecked(getState());//原来的全选
        let newBool = !allChecked;
        //修改全选 
        dispatch(actions.changeAllChecked(newBool));
        

        //修改list 
        let list = getList(getState());
        list.forEach(item => item.checked = newBool)
        dispatch(actions.changeList(list))
        // dispatch(actions.reqList())
    },
    //2.6 点了某一个
    changeOne: index => (dispatch, getState) => {
        //修改list
        console.log(index);
        
        let list = getList(getState());
        list[index].checked = !list[index].checked;
        dispatch(actions.changeList(list));
        //修改全选
        dispatch(actions.changeAllChecked(list.every(item => item.checked)))
        // dispatch(actions.reqList())
    }

}

//selector导出数据给组件
// 1.5 导出 
export const getList = state => state.shop.list;

// 2.5  导出allChecked 
export const getAllChecked = state => state.shop.allChecked;


// 3.1 总价 
export const getAllPrice = state => {
    let list = getList(state);
    return list.reduce(
        (v, i) => (i.checked ? v + i.price * i.num : v),
        0
    )
}
//导出reducer
export default reducer;